package com.bestcem.xm.user.util.business.captcha;

import cn.hutool.core.util.CharsetUtil;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 中文图片验证码对象
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/8 11:22
 */
@Getter
@Setter
public class ChineseCaptcha extends Captcha {

    /**
     * 中文图片验证码对象无参构造器
     *
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/8 14:01
     */
    public ChineseCaptcha() {
        super();
        // 中文图片验证码特殊配置
        this.setWidth(110);
        this.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, this.getFontSize()));
    }

    @Override
    public String randomCode(Random ran) throws UnsupportedEncodingException {
        // B0 + 0~39(16~55) 一级汉字所占区
        int highCode = (176 + Math.abs(ran.nextInt(39)));
        // A1 + 0~93 每区有94个汉字
        int lowCode = (161 + Math.abs(ran.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        return new String(b, CharsetUtil.GBK);
    }

}
